package com.ht.api.task.timer;

import cn.hutool.json.JSONObject;
import com.ht.api.db.*;
import com.ht.api.db.task.HtTaskExecutor;
import com.ht.api.db.task.TaskHandler;
import com.ht.api.service.TagService;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;

@Slf4j
public class OrderCountToUserTimerTask implements TaskHandler{

	@Override
	public String getName() {
		return "刷新用户订单数据";
	}

	@Override
	public boolean handler(JSONObject requestParam, String groupId, long taskId) {



		String sql = """
				INSERT INTO ecommerce_order_status
				(main_id, total_orders,unpaid_orders,unshipped_orders,
				unreceived_orders,completed_orders,cancelled_orders,
				after_sale_orders,refunded_orders)
				    
				SELECT
					o.oneid,
					COUNT(1) AS all_num,
					SUM(IF(i.order_status = '10', 1, 0)) AS pending_num,
					SUM(IF(i.order_status = '20', 1, 0)) AS paid_num,
					SUM(IF(i.order_status = '30', 1, 0)) AS shipped_num,
					SUM(IF(i.order_status = '40', 1, 0)) AS completed_num,
					SUM(IF(i.order_status = '50', 1, 0)) AS cancelled_num,
					SUM(IF(i.after_sale_status = '1', 1, 0)) AS after_sale_num,
					SUM(IF(i.refund_amount > 0, 1, 0)) AS refunded_num
					FROM
						`order` i
							LEFT JOIN user_origin o ON i.openid = o.openid
						GROUP BY
							o.oneid
				""";

		return true;
	}
	


}
